package com.anxin.registerManagement.service;

import cn.hutool.core.util.ObjectUtil;
import com.anxin.registerManagement.bean.jo.SceneSearchJo;
import com.anxin.registerManagement.bean.persist.Scene;
import com.anxin.registerManagement.exception.BusinessException;
import com.anxin.registerManagement.mapper.SceneMapper;
import com.anxin.registerManagement.util.Const;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Base64;
import java.util.List;

/**
 * @author anxin
 * @date 2022/3/30 19:27
 */
@Service
public class SceneService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SceneService.class);

    @Autowired
    private SceneMapper sceneMapper;

    public PageInfo<Scene> searchScene(SceneSearchJo sceneSearchJo) {
        if (!ObjectUtil.isEmpty(sceneSearchJo.getPageNum()) && !ObjectUtil.isEmpty(sceneSearchJo.getPageSize())) {
            PageHelper.startPage(sceneSearchJo.getPageNum(), sceneSearchJo.getPageSize());
        }
        LambdaQueryWrapper<Scene> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtil.isEmpty(sceneSearchJo.getSceneName())) {
            lambdaQueryWrapper.like(Scene::getSceneName, sceneSearchJo.getSceneName());
        }
        List<Scene> scenes = sceneMapper.selectList(lambdaQueryWrapper);
        return new PageInfo<>(scenes);
    }

    public void createScene(Scene scene) {
        checkScene(scene);
        sceneMapper.insert(scene);
    }

    public void updateScene(Scene scene) {
        checkScene(scene);
        sceneMapper.updateById(scene);
    }

    public void deleteScene(List<Integer> id) {
        sceneMapper.deleteBatchIds(id);
    }

    public List<Scene> getOtherById(Integer id) {
        LambdaQueryWrapper<Scene> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.notIn(Scene::getId, id);
        return sceneMapper.selectList(lambdaQueryWrapper);

    }

    /**
     * 公共数据校验方法
     */
    public void checkScene(Scene scene) {
        // 场景名称唯一
        LambdaQueryWrapper<Scene> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Scene::getSceneName, scene.getSceneName());
        Scene queryScene = sceneMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(scene.getId())) {
            // id为空，添加操作
            if (!ObjectUtils.isEmpty(queryScene)) {
                throw new BusinessException(Const.SCENE_EXIST);
            } else {
                if (!ObjectUtils.isEmpty(queryScene) && !scene.getId().equals(queryScene.getId())) {
                    throw new BusinessException(Const.SCENE_EXIST);
                }
            }
        }
    }
}
